<?php 
	session_start();
	if($_POST){
		$oneNum = $_POST['oneNumber'];
		$oper = $_POST['operation'];
		$twoNum = $_POST['twoNumber'];
		$error = "";
		$redirect = false;
		if(is_numeric($oneNum) && is_numeric($twoNum)){
			if($oper == 'plus'){
				$_SESSION['resl'] = intval($oneNum) + intval($twoNum);
				$oper = "+";
				$redirect = true;
			}else if($oper == 'minus'){
				$_SESSION['resl'] = intval($oneNum) -  intval($twoNum);
				$oper = "-";
				$redirect = true;
			}else if($oper == 'multyply'){
				$_SESSION['resl'] = intval($oneNum) *  intval($twoNum);
				$oper = "*";
				$redirect = true;
			}else if($oper == 'divide' && $twoNum != 0){
				$_SESSION['resl'] = intval($oneNum) /  intval($twoNum);
				$oper = "/";
				$redirect = true;
			}else{
				$error = "Введены некорректные данные";
			}
			$_SESSION['nums'] = [
				'one' => $oneNum,
				'two' => $twoNum,
				'oper' => $oper,
			];
		}else{
			$error = "Введены некорректные данные";
		}
	}
?>

<!DOCTYPE html>
<html lang="ru">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Калькулятор</title>
	<link rel="stylesheet" href="../css/style.css">
	<link rel="shortcut icon" href="https://bsquare.co.in/assets/php_icon.png">
</head>
<body>
	<div class="content-wraper">
		<div class="content">
			<h1 class="title">Калькулятор</h1>
			<form action="#" method="POST">
				<label for="oneNum">Первое число:</label>
				<input type="text" name="oneNumber" id="oneNum" required><br>
				<label for="oper">Операция</label>
				<select name="operation" id="oper">
					<option value="plus">+</option>
					<option value="minus">-</option>
					<option value="multyply">*</option>
					<option value="divide">/</option>
				</select><br>
				<label for="twoNum">Второе число:</label>
				<input type="text" name="twoNumber" id="twoNum" required><br>
				<button type="submit">Отправить</button>
				<button type="reset">Очистить</button>
				<a href="/"><button type="button">На главную</button></a><br>
				<span style="color: red;"> <?php echo $error; ?> </span>
			</form>
		</div>
	</div>
	<?php 
		if ($redirect){
			$redirect = false;
			echo '<script type="text/javascript">window.open("result.php","", "scrollbars=1");</script>';
		}
	?>
</body>
</html>
